<template>
  <el-dialog
    :title="!dataForm.bulidingId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="经纪人id" prop="buildingBrokerId">
      <el-input v-model="dataForm.buildingBrokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="buildingUserId">
      <el-input v-model="dataForm.buildingUserId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="房源名称" prop="buildingName">
      <el-input v-model="dataForm.buildingName" placeholder="房源名称"></el-input>
    </el-form-item>
    <el-form-item label="房源头像" prop="buildingImg">
      <el-input v-model="dataForm.buildingImg" placeholder="房源头像"></el-input>
    </el-form-item>
    <el-form-item label="房源类型" prop="buildingType">
      <el-input v-model="dataForm.buildingType" placeholder="房源类型"></el-input>
    </el-form-item>
    <el-form-item label="租房类型" prop="bulidingRentingType">
      <el-input v-model="dataForm.bulidingRentingType" placeholder="租房类型"></el-input>
    </el-form-item>
    <el-form-item label="房源价格" prop="bulidingPrice">
      <el-input v-model="dataForm.bulidingPrice" placeholder="房源价格"></el-input>
    </el-form-item>
    <el-form-item label="房源总价" prop="bulidingSumPrice">
      <el-input v-model="dataForm.bulidingSumPrice" placeholder="房源总价"></el-input>
    </el-form-item>
    <el-form-item label="房源面积" prop="bulidingArea">
      <el-input v-model="dataForm.bulidingArea" placeholder="房源面积"></el-input>
    </el-form-item>
    <el-form-item label="房源租费" prop="bulidingRent">
      <el-input v-model="dataForm.bulidingRent" placeholder="房源租费"></el-input>
    </el-form-item>
    <el-form-item label="房源规格" prop="bulidingSpecification">
      <el-input v-model="dataForm.bulidingSpecification" placeholder="房源规格"></el-input>
    </el-form-item>
    <el-form-item label="省份" prop="bulidingAddrSid">
      <el-input v-model="dataForm.bulidingAddrSid" placeholder="省份"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="bulidingAddrCid">
      <el-input v-model="dataForm.bulidingAddrCid" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="区" prop="bulidingAddrQid">
      <el-input v-model="dataForm.bulidingAddrQid" placeholder="区"></el-input>
    </el-form-item>
    <el-form-item label="小区" prop="bulidingPlot">
      <el-input v-model="dataForm.bulidingPlot" placeholder="小区"></el-input>
    </el-form-item>
    <el-form-item label="楼层" prop="bulidingFloor">
      <el-input v-model="dataForm.bulidingFloor" placeholder="楼层"></el-input>
    </el-form-item>
    <el-form-item label="朝向" prop="bulidingOrientation">
      <el-input v-model="dataForm.bulidingOrientation" placeholder="朝向"></el-input>
    </el-form-item>
    <el-form-item label="房子类型" prop="bulidingGenre">
      <el-input v-model="dataForm.bulidingGenre" placeholder="房子类型"></el-input>
    </el-form-item>
    <el-form-item label="配套信息" prop="bulidingAssortInfo">
      <el-input v-model="dataForm.bulidingAssortInfo" placeholder="配套信息"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="bulidingStatus">
      <el-input v-model="dataForm.bulidingStatus" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="bulidingCreateTime">
      <el-input v-model="dataForm.bulidingCreateTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="bulidingUpdateTime">
      <el-input v-model="dataForm.bulidingUpdateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bulidingId: 0,
          buildingBrokerId: '',
          buildingUserId: '',
          buildingName: '',
          buildingImg: '',
          buildingType: '',
          bulidingRentingType: '',
          bulidingPrice: '',
          bulidingSumPrice: '',
          bulidingArea: '',
          bulidingRent: '',
          bulidingSpecification: '',
          bulidingAddrSid: '',
          bulidingAddrCid: '',
          bulidingAddrQid: '',
          bulidingPlot: '',
          bulidingFloor: '',
          bulidingOrientation: '',
          bulidingGenre: '',
          bulidingAssortInfo: '',
          bulidingStatus: '',
          bulidingCreateTime: '',
          bulidingUpdateTime: ''
        },
        dataRule: {
          buildingBrokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ],
          buildingUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          buildingName: [
            { required: true, message: '房源名称不能为空', trigger: 'blur' }
          ],
          buildingImg: [
            { required: true, message: '房源头像不能为空', trigger: 'blur' }
          ],
          buildingType: [
            { required: true, message: '房源类型不能为空', trigger: 'blur' }
          ],
          bulidingRentingType: [
            { required: true, message: '租房类型不能为空', trigger: 'blur' }
          ],
          bulidingPrice: [
            { required: true, message: '房源价格不能为空', trigger: 'blur' }
          ],
          bulidingSumPrice: [
            { required: true, message: '房源总价不能为空', trigger: 'blur' }
          ],
          bulidingArea: [
            { required: true, message: '房源面积不能为空', trigger: 'blur' }
          ],
          bulidingRent: [
            { required: true, message: '房源租费不能为空', trigger: 'blur' }
          ],
          bulidingSpecification: [
            { required: true, message: '房源规格不能为空', trigger: 'blur' }
          ],
          bulidingAddrSid: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
          bulidingAddrCid: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          bulidingAddrQid: [
            { required: true, message: '区不能为空', trigger: 'blur' }
          ],
          bulidingPlot: [
            { required: true, message: '小区不能为空', trigger: 'blur' }
          ],
          bulidingFloor: [
            { required: true, message: '楼层不能为空', trigger: 'blur' }
          ],
          bulidingOrientation: [
            { required: true, message: '朝向不能为空', trigger: 'blur' }
          ],
          bulidingGenre: [
            { required: true, message: '房子类型不能为空', trigger: 'blur' }
          ],
          bulidingAssortInfo: [
            { required: true, message: '配套信息不能为空', trigger: 'blur' }
          ],
          bulidingStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          bulidingCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          bulidingUpdateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bulidingId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bulidingId) {
            this.$http({
              url: this.$http.adornUrl(`/building/building/info/${this.dataForm.bulidingId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.buildingBrokerId = data.building.buildingBrokerId
                this.dataForm.buildingUserId = data.building.buildingUserId
                this.dataForm.buildingName = data.building.buildingName
                this.dataForm.buildingImg = data.building.buildingImg
                this.dataForm.buildingType = data.building.buildingType
                this.dataForm.bulidingRentingType = data.building.bulidingRentingType
                this.dataForm.bulidingPrice = data.building.bulidingPrice
                this.dataForm.bulidingSumPrice = data.building.bulidingSumPrice
                this.dataForm.bulidingArea = data.building.bulidingArea
                this.dataForm.bulidingRent = data.building.bulidingRent
                this.dataForm.bulidingSpecification = data.building.bulidingSpecification
                this.dataForm.bulidingAddrSid = data.building.bulidingAddrSid
                this.dataForm.bulidingAddrCid = data.building.bulidingAddrCid
                this.dataForm.bulidingAddrQid = data.building.bulidingAddrQid
                this.dataForm.bulidingPlot = data.building.bulidingPlot
                this.dataForm.bulidingFloor = data.building.bulidingFloor
                this.dataForm.bulidingOrientation = data.building.bulidingOrientation
                this.dataForm.bulidingGenre = data.building.bulidingGenre
                this.dataForm.bulidingAssortInfo = data.building.bulidingAssortInfo
                this.dataForm.bulidingStatus = data.building.bulidingStatus
                this.dataForm.bulidingCreateTime = data.building.bulidingCreateTime
                this.dataForm.bulidingUpdateTime = data.building.bulidingUpdateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/building/building/${!this.dataForm.bulidingId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bulidingId': this.dataForm.bulidingId || undefined,
                'buildingBrokerId': this.dataForm.buildingBrokerId,
                'buildingUserId': this.dataForm.buildingUserId,
                'buildingName': this.dataForm.buildingName,
                'buildingImg': this.dataForm.buildingImg,
                'buildingType': this.dataForm.buildingType,
                'bulidingRentingType': this.dataForm.bulidingRentingType,
                'bulidingPrice': this.dataForm.bulidingPrice,
                'bulidingSumPrice': this.dataForm.bulidingSumPrice,
                'bulidingArea': this.dataForm.bulidingArea,
                'bulidingRent': this.dataForm.bulidingRent,
                'bulidingSpecification': this.dataForm.bulidingSpecification,
                'bulidingAddrSid': this.dataForm.bulidingAddrSid,
                'bulidingAddrCid': this.dataForm.bulidingAddrCid,
                'bulidingAddrQid': this.dataForm.bulidingAddrQid,
                'bulidingPlot': this.dataForm.bulidingPlot,
                'bulidingFloor': this.dataForm.bulidingFloor,
                'bulidingOrientation': this.dataForm.bulidingOrientation,
                'bulidingGenre': this.dataForm.bulidingGenre,
                'bulidingAssortInfo': this.dataForm.bulidingAssortInfo,
                'bulidingStatus': this.dataForm.bulidingStatus,
                'bulidingCreateTime': this.dataForm.bulidingCreateTime,
                'bulidingUpdateTime': this.dataForm.bulidingUpdateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
